<?php
/**
 * Theme settings functions
 *
 * @package one
 * @subpackage Admin
 */

// Add theme settings page to the WP admin menu
	add_action('admin_menu', 'one_add_pages');

/**
 * Gets all theme admin menu pages
 * Creates a page called 'theme-settings.php' in the admin
 *
 * @since 0.2
 */
function one_add_pages() {
	add_theme_page(__('one Theme Settings','one'), __('主题设置','one'), 10, 'theme-settings.php', one_theme_page);
}

/**
 * Returns an array of the theme settings
 * Add all options to a single array
 * This makes one entry in the database
 *
 * @since 0.4
 * @return array all options for theme settings
 */
function one_theme_settings() {
	$settings_arr = array(
		'feed_url' => false, // General settings
		'feeds_redirect' => false,
		'print_style' => true,
		'page_nav' => array(),
		'auto_first_class' => false,

		'pullquotes_js' => false, // JavaScript settings
		'superfish_js' => false,
		'hoverintent_js' => false,
		'supersubs_js' => false,
		'bgiframe_js' => false,

		'primary_inserts_default' => true, // Widget settings
		'secondary_inserts_default' => false,
		'subsidiary_inserts_default' => false,

		'robots_home' => true, // Indexing
		'robots_single' => true,
		'robots_attachment' => true,
		'robots_page' => true,
		'robots_date' => true,
		'robots_category' => true,
		'robots_tag' => true,
		'robots_author' => true,
		'robots_search' => true,
		'robots_404' => true,

		'seo_cats' => true, // SEO
		'seo_tags' => true,
		'seo_single_excerpts' => true,
		'seo_author' => true,
		'seo_category' => true,
		'seo_meta_author' => true,

		'seo_blog_title' => false,
		'seo_show_tagline' => true,
		'seo_tagline_before_title' => false,

		'default_avatar' => false, // Avatars

		'footer_insert' => false, // Footer settings
		'copyright' => true,
		'wp_credit' => true,
		'th_credit' => true,
		'query_counter' => false,
	);

	return apply_filters('one_settings_args', $settings_arr);
}

/**
 * Handles the main theme settings
 * Creates the form for saving the theme settings
 * Loads the theme settings page(s)
 * Updates the theme settings in the database if necessary
 *
 * @since 0.2
 */
function one_theme_page() {

	/*
	* Variables to be used throughout the settings page
	*/
	$theme_name = __('one','one');
	$settings_page_title = __('one Theme Settings','one');
	$hidden_field_name = 'one_submit_hidden';
	$theme_data = get_theme_data(TEMPLATEPATH . '/style.css');

	/*
	* Get the theme settings and add them to the database
	*/
	$settings_arr = one_theme_settings();
	add_option('one_theme_settings', $settings_arr);

	/*
	* Set form data IDs the same as settings keys
	*/
	$settings_keys = array_keys($settings_arr);
	foreach($settings_keys as $key) :
		$data[$key] = $key;
	endforeach;

	/*
	* Get existing options from the database
	*/
	$settings = get_option('one_theme_settings');

	foreach($settings_arr as $key => $value) :
		$val[$key] = $settings[$key];
	endforeach;

	/*
	* If the form has been set
	* Loop through the values
	* Set the option in the database
	*/
	if($_POST[$hidden_field_name] == 'Y') :

		foreach($settings_arr as $key => $value) :
			$settings[$key] = $val[$key] = $_POST[$data[$key]];
		endforeach;

		update_option('one_theme_settings', $settings);

		/*
		* Open main div for the theme settings
		*/
		echo '<div class="wrap">';
		echo '<h2>' . $settings_page_title . '</h2>';
		echo '<div class="updated" style="margin:15px 0;">';
		echo '<p><strong>' . __('Settings saved.','one') . '</strong></p>';
		echo '</div>';

	else :
		echo '<div class="wrap">';
		echo '<h2>' . $settings_page_title . '</h2>';

	endif;

	/*
	* Load the theme settings form
	*/
	if($theme_data['URI'] !== 'http://onefrozen.com/theme' || $theme_data['Name'] !== 'one') :
		one_error();
		//return;
	endif;
?>

		<div id="poststuff" class="dlm">

			<form name="form0" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" style="border:none;background:transparent;">

			<?php
				include('about.php');
				include('general.php');
			?>

			<p class="submit">
				<input type="submit" name="Submit"  class="button-primary" value="<?php _e('Save Changes', 'one' ) ?>" />
				<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y" />
			</p>

			</form>
		</div>
	</div>
<?php
}

?>